package com.service.impl;

import com.mapper.NoteTagsMapper;
import com.pojo.NoteTags;
import com.pojo.Result;
import com.service.NoteTagsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class NoteTagsServiceImpl implements NoteTagsService {

    @Autowired
    private NoteTagsMapper noteTagsMapper;

    @Override
    public Result<Void> addTagToNote(Integer noteId, Integer tagId) {
        try {
            // 检查关联是否已存在
            if (noteTagsMapper.exists(noteId, tagId) > 0) {
                return Result.error("该标签已关联到此笔记");
            }

            // 创建关联
            NoteTags noteTag = new NoteTags();
            noteTag.setNoteId(noteId);
            noteTag.setTagId(tagId);
            noteTag.setCreatedAt(LocalDateTime.now());

            int result = noteTagsMapper.insert(noteTag);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.error("添加标签到笔记失败");
            }
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<Void> removeTagFromNote(Integer noteId, Integer tagId) {
        try {
            int result = noteTagsMapper.delete(noteId, tagId);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.error("移除标签失败，可能关联不存在");
            }
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<Void> removeAllTagsFromNote(Integer noteId) {
        try {
            int result = noteTagsMapper.deleteByNoteId(noteId);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.success(); // 没有标签也算成功
            }
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<Void> removeTagFromAllNotes(Integer tagId) {
        try {
            int result = noteTagsMapper.deleteByTagId(tagId);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.success(); // 没有关联也算成功
            }
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<List<Integer>> getTagsByNoteId(Integer noteId) {
        try {
            List<Integer> tagIds = noteTagsMapper.selectTagIdsByNoteId(noteId);
            return Result.success(tagIds);
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<List<Integer>> getNotesByTagId(Integer tagId) {
        try {
            List<Integer> noteIds = noteTagsMapper.selectNoteIdsByTagId(tagId);
            return Result.success(noteIds);
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<Boolean> isTagAttachedToNote(Integer noteId, Integer tagId) {
        try {
            int exists = noteTagsMapper.exists(noteId, tagId);
            return Result.success(exists > 0);
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }
}